package com.gtr.controller;


import com.alibaba.excel.EasyExcel;
import com.gtr.entity.VaccinumInfo;
import com.gtr.form.SellerVaccinumInfoForm;
import com.gtr.form.SellerVaccinumInfoUpdateForm;
import com.gtr.handler.CustomCellWriteHandler;
import com.gtr.service.VaccinumCategoryService;
import com.gtr.service.VaccinumInfoService;
import com.gtr.util.ResultVOUtil;
import com.gtr.vo.ResultVO;
import com.gtr.vo.VaccinumExcelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 疫苗表 前端控制器
 * </p>
 *
 * @author gtr
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/seller/vaccinum")
public class SellerVaccinumController {

    @Autowired
    private VaccinumCategoryService vaccinumCategoryService;
    @Autowired
    private VaccinumInfoService vaccinumInfoService;


    @GetMapping("/findAllVaccinumCategory")
    public ResultVO findAllVaccinumCategory(){ //查询所有疫苗的分类
        Map<String, List> map = new HashMap<>();
        map.put("content",this.vaccinumCategoryService.sellerList());
        return ResultVOUtil.success(map);
    }

    @PostMapping("/add")
    public ResultVO add(@RequestBody SellerVaccinumInfoForm form){
        VaccinumInfo vaccinumInfo = new VaccinumInfo();
        BeanUtils.copyProperties(form, vaccinumInfo);
        this.vaccinumInfoService.save(vaccinumInfo);
        return ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVO list(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        return ResultVOUtil.success(this.vaccinumInfoService.sellerVaccinumInfoVO(page, size));
    }

    @GetMapping("/like/{keyWord}/{page}/{size}")
    public ResultVO like(
            @PathVariable("keyWord") String keyWord,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        return ResultVOUtil.success(this.vaccinumInfoService.sellerVaccinumInfoVOLike(keyWord,page, size));
    }

    @GetMapping("/findByCategory/{categoryType}/{page}/{size}")
    public ResultVO findByCategory(
            @PathVariable("categoryType") Integer categoryType,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        return ResultVOUtil.success(this.vaccinumInfoService.sellerVaccinumInfoVOByCategoryType(categoryType, page, size));
    }

    @GetMapping("/findById/{id}")
    public ResultVO findById(
            @PathVariable("id") Integer id
    ){
        return ResultVOUtil.success(this.vaccinumInfoService.sellerVaccinumInfoVOById(id));
    }

    @DeleteMapping("/delete/{id}")
    public ResultVO delete(@PathVariable("id") Integer id){
        this.vaccinumInfoService.removeById(id);
        return ResultVOUtil.success(null);
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public ResultVO updateStatus(@PathVariable("id") Integer id,
                                 @PathVariable("status") Boolean status){
        VaccinumInfo vaccinumInfo = new VaccinumInfo();
        vaccinumInfo.setVaccinumId(id);
        if (status) {
            vaccinumInfo.setVaccinumStatus(1);
        } else {
            vaccinumInfo.setVaccinumStatus(0);
        }
        this.vaccinumInfoService.updateById(vaccinumInfo);
        return ResultVOUtil.success(null);
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody SellerVaccinumInfoUpdateForm form){
        VaccinumInfo vaccinumInfo = new VaccinumInfo();
        BeanUtils.copyProperties(form, vaccinumInfo);
        if(form.getStatus()){
            vaccinumInfo.setVaccinumStatus(1);
        } else {
            vaccinumInfo.setVaccinumStatus(0);
        }
        vaccinumInfo.setCategoryType(form.getCategory().getCategoryType());
        this.vaccinumInfoService.updateById(vaccinumInfo);
        return ResultVOUtil.success(null);
    }

    @GetMapping("/export")
    public void exportData(HttpServletResponse response){
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("疫苗信息", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //获取VaccinumExcelVO类型的List
            List<VaccinumExcelVO> vaccinumExcelVOList = this.vaccinumInfoService.excelVOList();
            EasyExcel.write(response.getOutputStream(), VaccinumExcelVO.class)
                    .registerWriteHandler(new CustomCellWriteHandler())
                    .sheet("疫苗信息")
                    .doWrite(vaccinumExcelVOList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/import")
    public ResultVO importData(@RequestParam("file") MultipartFile file){
        List<VaccinumInfo> vaccinumInfoList = null;
        try {
            vaccinumInfoList = this.vaccinumInfoService.excleToVaccinumInfoList(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(vaccinumInfoList==null){
            return ResultVOUtil.fail("导入Excel失败！");
        }
        boolean result = this.vaccinumInfoService.saveBatch(vaccinumInfoList);
        if(result)return ResultVOUtil.success(null);
        return ResultVOUtil.fail("导入Excel失败！");
    }
}

